//
//  PigRecordView.m
//  PigMarket
//
//  Created by mac on 2019/7/3.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PigRecordCell.h"

@interface PigRecordCell ()
/**
 猪的照片
 */
@property (nonatomic , strong) UIImageView *pigImg;
/**
 leftTtile
 */
@property (nonatomic , strong) UILabel *leftTitle;
/**
 右侧内容
 */
@property (nonatomic , strong) YYLabel *detailLab;
/**
 灰线
 */
@property (nonatomic , strong) UIView *line;
/**
 箭头
 */
@property (nonatomic , strong) UIImageView *accessImg;
@end
@implementation PigRecordCell
+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    PigRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[PigRecordCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.leftTitle];
    [self.contentView addSubview:self.detailLab];
    [self.contentView addSubview:self.pigImg];
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.accessImg];
}

- (void)initLayout {
    // 布局子控件
    [self.leftTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(27);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(15);
    }];
    [self.accessImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-27);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
    [self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.accessImg.mas_left);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(26);
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(27);
        make.right.mas_equalTo(-27);
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(0);
    }];
    [self.pigImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-1);
    }];
}
- (void)configCellWithModel:(PigRecord *)model index:(NSIndexPath *)index {
    if (index.row == 0) {
        self.pigImg.hidden = YES;
        self.leftTitle.hidden = NO;
        self.detailLab.hidden = NO;
        self.accessImg.hidden = NO;
     
    } else if (index.row == 1){
        self.pigImg.hidden = NO;
        self.leftTitle.hidden = YES;
        self.detailLab.hidden = YES;
        self.accessImg.hidden = YES;
    
        [self.detailLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-27);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.height.mas_equalTo(26);
        }];
    } else {
        self.pigImg.hidden = YES;
        self.leftTitle.hidden = NO;
        self.detailLab.hidden = NO;
        self.accessImg.hidden = YES;
        [self.detailLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-27);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.height.mas_equalTo(26);
        }];
    }
    
    if (index.row == 0) {
        self.leftTitle.text = model.varietiesName;
        [self configDetailWithStr:@"生长报告" detailFont:12 detailColor:@"#53C203" leftFont:16 leftColor:@"#222222"];
    } else if (index.row == 1) {
        [self.pigImg sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_middle"]];
    } else if (index.row == 2) {
        self.leftTitle.text = @"仔猪编号";
        [self configDetailWithStr:model.pigNum detailFont:15 detailColor:@"#262626" leftFont:15 leftColor:@"#666666"];
    } else if (index.row == 3) {
        self.leftTitle.text = @"出生日期";
        [self configDetailWithStr:[NSString stringWithFormat:@"%@",model.birthDate] detailFont:15 detailColor:@"#262626" leftFont:15 leftColor:@"#666666"];
    } else if (index.row == 4) {
        self.leftTitle.text = @"领养日期";
        [self configDetailWithStr:model.adoptTime detailFont:15 detailColor:@"#262626" leftFont:15 leftColor:@"#666666"];
    } else if (index.row == 5) {
        self.leftTitle.text = @"当前日龄";
        [self configDetailWithStr:[NSString stringWithFormat:@"%@日",model.dayAge] detailFont:15 detailColor:@"#262626" leftFont:15 leftColor:@"#666666"];
    } else if (index.row == 6) {
        self.leftTitle.text = @"仔猪体重";
        [self configDetailWithStr:[NSString stringWithFormat:@"%.2fkg(%@)",model.weight,model.weighDate] detailFont:15 detailColor:@"#262626" leftFont:15 leftColor:@"#666666"];
    } else if (index.row == 7) {
        self.leftTitle.text = @"预出栏日";
        [self configDetailWithStr:[NSString stringWithFormat:@"%@",model.expectOutDate] detailFont:15 detailColor:@"#262626" leftFont:15 leftColor:@"#666666"];
    } else if (index.row == 8) {
        self.leftTitle.text = @"欠费天数";
        NSString *str = [NSString stringWithFormat:@"%@天",model.arrearsDays];
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
        text.yy_color = [UIColor colorWithHexString:@"#A1A1A1"];
        text.yy_font = [UIFont systemFontOfSize:12];
        [text yy_setColor:[UIColor colorWithHexString:@"#F25C62"] range:NSMakeRange(0, str.length - 1)];
        [text yy_setFont:[UIFont systemFontOfSize:22] range:NSMakeRange(0, str.length - 1)];
        self.detailLab.attributedText = text;
        self.leftTitle.textColor = [UIColor colorWithHexString:@"#666666"];
        self.leftTitle.font = [UIFont systemFontOfSize:15];
     }
}
- (void)configDetailWithStr:(NSString *)str detailFont:(CGFloat)detailFont detailColor:(NSString *)detailColor leftFont:(CGFloat)leftFont leftColor:(NSString *)leftColor{
  
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
    text.yy_font = [UIFont systemFontOfSize:detailFont];
    text.yy_color = [UIColor colorWithHexString:detailColor];
    self.detailLab.attributedText = text;
    
    self.leftTitle.textColor = [UIColor colorWithHexString:leftColor];
    self.leftTitle.font = [UIFont systemFontOfSize:leftFont];
}
#pragma mark - getter and setter
- (UILabel *)leftTitle {
    if (!_leftTitle) {
        _leftTitle = [[UILabel alloc] init];
    }
    return _leftTitle;
}
- (YYLabel *)detailLab {
    if (!_detailLab) {
        _detailLab = [[YYLabel alloc] init];
        _detailLab.textAlignment = NSTextAlignmentRight;
    }
    return _detailLab;
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    }
    return _line;
}
- (UIImageView *)pigImg {
    if (!_pigImg) {
        _pigImg = [[UIImageView alloc] init];
        _pigImg.hidden = YES;
    }
    return _pigImg;
}
- (UIImageView *)accessImg {
    if (!_accessImg) {
        _accessImg = [[UIImageView alloc] init];
        _accessImg.image = [UIImage imageNamed:@"nav_green"];
        _accessImg.hidden = YES;
    }
    return _accessImg;
}
@end
